package com.zhisheng.weekdaytest.controller;

import com.zhisheng.weekdaytest.entry.MyAvg;
import com.zhisheng.weekdaytest.mapper.CDTMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin // 允许前端跨域请求
@RequestMapping("/cdt")
public class CDTController {

    @Autowired
    private CDTMapper cdtMapper;

    @GetMapping("select")
    public Map<String, List> selectAll(){
        List<MyAvg> allValue = cdtMapper.findAvgByGroupName();
        List<String> citys = new ArrayList<>();
        List<String> months = new ArrayList<>();
        List<Double> avgs = new ArrayList<>();

        for (MyAvg myAvg : allValue) {
            citys.add(myAvg.getCityName());
            months.add(myAvg.getMonth());
            avgs.add(myAvg.getAvgTemperature());
        }
        Map<String, List> map = new HashMap<>();
        map.put("city",citys);
        map.put("month",months);
        map.put("avg",avgs);
        return map;
    }
}
